package com.bookocean.infrastructure.persistence.converter;

import com.bookocean.controller.dto.NovelDto;
import com.bookocean.controller.dto.NovelChapterDto;
import com.bookocean.controller.dto.NovelParticipantDto;
import com.bookocean.infrastructure.persistence.po.NovelPO;
import com.bookocean.infrastructure.persistence.po.NovelChapterPO;
import com.bookocean.infrastructure.persistence.po.NovelParticipantPO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 小说数据转换器
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
public class NovelConverter {

    /**
     * PO转DTO
     */
    public static NovelDto toDto(NovelPO po) {
        if (po == null) {
            return null;
        }
        
        NovelDto dto = new NovelDto();
        dto.setId(po.getNovelId());
        dto.setTitle(po.getTitle());
        dto.setDescription(po.getDescription());
        dto.setTags(po.getGenre());
        dto.setCoverUrl(po.getCoverUrl());
        dto.setStatus(po.getStatus());
        dto.setCreationType(po.getIsMultiplayer() ? 2 : 1);
        dto.setTargetChapterCount(po.getMaxChapters());
        dto.setCurrentChapterCount(po.getChapterCount());
        dto.setCreatorId(po.getCreatorId());
        dto.setCreateTime(po.getCreatedAt());
        dto.setUpdateTime(po.getUpdatedAt());
        
        return dto;
    }

    /**
     * DTO转PO
     */
    public static NovelPO toPo(NovelDto dto) {
        if (dto == null) {
            return null;
        }
        
        NovelPO po = new NovelPO();
        po.setNovelId(dto.getId());
        po.setTitle(dto.getTitle());
        po.setDescription(dto.getDescription());
        po.setGenre(dto.getTags());
        po.setCoverUrl(dto.getCoverUrl());
        po.setStatus(dto.getStatus());
        po.setIsMultiplayer(dto.getCreationType() == 2);
        po.setMaxChapters(dto.getTargetChapterCount());
        po.setChapterCount(dto.getCurrentChapterCount());
        po.setCreatorId(dto.getCreatorId());
        po.setCreatedAt(dto.getCreateTime());
        po.setUpdatedAt(dto.getUpdateTime());
        
        return po;
    }

    /**
     * 章节PO转DTO
     */
    public static NovelChapterDto chapterToDto(NovelChapterPO po) {
        if (po == null) {
            return null;
        }
        
        NovelChapterDto dto = new NovelChapterDto();
        dto.setId(po.getChapterId());
        dto.setNovelId(po.getNovelId());
        dto.setTitle(po.getTitle());
        dto.setContent(po.getContent());
        dto.setChapterNumber(po.getChapterNumber());
        dto.setAuthorId(po.getAuthorId());
        dto.setCreateTime(po.getCreatedAt());
        
        return dto;
    }

    /**
     * 章节DTO转PO
     */
    public static NovelChapterPO chapterToPo(NovelChapterDto dto) {
        if (dto == null) {
            return null;
        }
        
        NovelChapterPO po = new NovelChapterPO();
        po.setChapterId(dto.getId());
        po.setNovelId(dto.getNovelId());
        po.setTitle(dto.getTitle());
        po.setContent(dto.getContent());
        po.setChapterNumber(dto.getChapterNumber());
        po.setAuthorId(dto.getAuthorId());
        po.setCreatedAt(dto.getCreateTime());
        
        return po;
    }

    /**
     * 参与者PO转DTO
     */
    public static NovelParticipantDto participantToDto(NovelParticipantPO po) {
        if (po == null) {
            return null;
        }
        
        NovelParticipantDto dto = new NovelParticipantDto();
        dto.setId(po.getParticipantId());
        dto.setNovelId(po.getNovelId());
        dto.setUserId(po.getUserId());
        dto.setParticipantOrder(po.getJoinOrder());
        dto.setIsCreator(po.getRole() == 1);
        dto.setChapterCount(po.getChapterCount());
        dto.setJoinTime(po.getJoinedAt());
        
        return dto;
    }

    /**
     * 参与者DTO转PO
     */
    public static NovelParticipantPO participantToPo(NovelParticipantDto dto) {
        if (dto == null) {
            return null;
        }
        
        NovelParticipantPO po = new NovelParticipantPO();
        po.setParticipantId(dto.getId());
        po.setNovelId(dto.getNovelId());
        po.setUserId(dto.getUserId());
        po.setJoinOrder(dto.getParticipantOrder());
        po.setRole(dto.getIsCreator() ? 1 : 2);
        po.setChapterCount(dto.getChapterCount());
        po.setJoinedAt(dto.getJoinTime());
        
        return po;
    }

    /**
     * 批量转换章节列表
     */
    public static List<NovelChapterDto> chaptersToDtoList(List<NovelChapterPO> poList) {
        if (poList == null || poList.isEmpty()) {
            return null;
        }
        return poList.stream()
                .map(NovelConverter::chapterToDto)
                .collect(Collectors.toList());
    }

    /**
     * 批量转换参与者列表
     */
    public static List<NovelParticipantDto> participantsToDtoList(List<NovelParticipantPO> poList) {
        if (poList == null || poList.isEmpty()) {
            return null;
        }
        return poList.stream()
                .map(NovelConverter::participantToDto)
                .collect(Collectors.toList());
    }
}
